<template>
  <div class="user-container">
    <!-- 固定的侧边栏 -->
    <div class="sidebar">
      <!-- 账号中心 -->
      <div class="menu-section">
        <div class="section-title">账号中心</div>
        <el-menu
          :default-active="activeMenu"
          class="menu-list"
          @select="handleSelect"
          :router="true"
        >
          <el-menu-item index="/user/about">
            <el-icon><Menu /></el-icon>
            <span>概览</span>
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 我的账号 -->
      <div class="menu-section">
        <div class="section-title">我的账号</div>
        <el-menu
          :default-active="activeMenu"
          class="menu-list"
          @select="handleSelect"
          :router="true"
        >
          <el-menu-item index="/user/information">
            <el-icon><User /></el-icon>
            <span>基本信息</span>
          </el-menu-item>
          <el-menu-item index="/user/health">
            <el-icon><List /></el-icon>
            <span>健康状态</span>
          </el-menu-item>
          <el-menu-item index="/user/family" v-if="userStore.userInfo?.familyRole">
            <el-icon><UserFilled /></el-icon>
            <span>家庭共享</span>
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 账号安全 -->
      <div class="menu-section">
        <div class="section-title">账号安全</div>
        <el-menu
          :default-active="activeMenu"
          class="menu-list"
          @select="handleSelect"
          :router="true"
        >
          <el-menu-item index="/user/account">
            <el-icon><Lock /></el-icon>
            <span>账号安全</span>
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 返回首页按钮 -->
      <div class="back-home">
        <el-button @click="goHome" type="primary" class="back-btn">
          <el-icon><Back /></el-icon>
          返回首页
        </el-button>
      </div>
    </div>

    <!-- 可滚动的主内容区 -->
    <div class="main-content">
      <router-view></router-view>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { Menu, User, List, Lock, Back, UserFilled } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const activeMenu = computed(() => route.path)

const handleSelect = (index) => {
  router.push(index)
}

const goHome = () => {
  router.push('/')
}
</script>

<style scoped>
.user-container {
  display: flex;
  min-height: 100vh;
  background-color: #f5f7fa;
}

.sidebar {
  width: 240px;
  background-color: white;
  padding: 20px 0;
  box-shadow: 2px 0 8px rgba(0,0,0,0.05);
  display: flex;
  flex-direction: column;
  position: fixed;  /* 固定侧边栏 */
  left: 0;
  top: 0;
  bottom: 0;
  overflow-y: auto;  /* 如果内容过多允许滚动 */
}

.menu-section {
  margin-bottom: 20px;
}

.section-title {
  padding: 0 20px;
  margin-bottom: 8px;
  color: #909399;
  font-size: 12px;
}

.menu-list {
  border-right: none;
}

.main-content {
  flex: 1;
  padding: 20px;
  margin-left: 240px;  /* 为固定侧边栏留出空间 */
  overflow-y: auto;  /* 允许内容区滚动 */
  min-height: 100vh;
  box-sizing: border-box;
}

.back-home {
  margin-top: auto;
  padding: 20px;
  background-color: white;
}

.back-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

:deep(.el-menu-item) {
  height: 40px;
  line-height: 40px;
}

:deep(.el-menu-item.is-active) {
  background-color: #ecf5ff;
  color: #409EFF;
}

:deep(.el-menu-item:hover) {
  background-color: #f5f7fa;
}

.el-icon {
  margin-right: 8px;
}

/* 优化滚动条样式 */
.sidebar::-webkit-scrollbar,
.main-content::-webkit-scrollbar {
  width: 6px;
}

.sidebar::-webkit-scrollbar-thumb,
.main-content::-webkit-scrollbar-thumb {
  background-color: #dcdfe6;
  border-radius: 3px;
}

.sidebar::-webkit-scrollbar-track,
.main-content::-webkit-scrollbar-track {
  background-color: transparent;
}
</style>
